#include "kvs.h"

int kvs_skiplist_create(skiplist_t **sl)
{
    if (sl == NULL)
        return -1;
    if (*sl)
        return -1;

    *sl = skiplist_init();
    if (*sl == NULL)
        return -1;

#if (FULL_PERSIST && INIT_BY_FULL)
    LOG("正在同步跳表全量持久化: %s\n", SKIPLIST_FULL_FILE_NALE);
    int ret = skiplist_full_restore(SKIPLIST_FULL_FILE_NALE, *sl);
    if (ret == -1)
    {
        LOG("跳表全量持久化失败, 建立空的红黑树\n");
    }
    else
    {
        LOG("跳表全量持久化成功\n");
    }
#endif

    return 0;
}

int kvs_skiplist_destroy(skiplist_t *sl)
{
    if (sl == NULL)
        return -1;
#if (FULL_PERSIST && STORE_FULL)
    // 全量持久化到文件
#if BACK_UP_OLD_FULL_PERSIST_FILE
    if (skiplist_full_store_with_backup(sl, SKIPLIST_FULL_FILE_NALE) == 0)
    {
        printf("跳表全量文件保存成功\n");
    }
#else
    if (skiplist_full_store(sl, SKIPLIST_FULL_FILE_NALE) == 0)
    {
        printf("跳表全量文件保存成功\n");
    }
#endif
#endif

    skiplist_destroy(sl);
    return 0;
}

int kvs_skiplist_set(skiplist_t *sl, char *key, char *value)
{
    if (!sl || !key || !value)
        return -1;
    int ret = skiplist_insert(sl, key, value);
    if (ret == 0)
    {
        // 成功
#if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "SSET", key, value);
        if (res == 0)
        {
            // LOG("[AOF]: SSET ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: SSET ASYN FAIL\n");
        }
#endif
        return 0;
    }
    if (ret == -1)
    {
        return -1;
    }

    if (ret == 1)
    {
        // 键存在
        return 1;
    }
}

char *kvs_skiplist_get(skiplist_t *sl, char *key)
{

    if (!sl || !key)
        return NULL;
    SNode *buf = skiplist_search(sl, key);
    if (buf == NULL)
    {
        return NULL;
    }
    return buf->value;
}

int kvs_skiplist_del(skiplist_t *sl, char *key)
{
    if (!sl || !key)
        return -1;

    int ret = skiplist_delete(sl, key);

    if (ret == 1)
    {
        // 没找到
        return 1;
    }
    if (ret == -1)
    {
        return -1;
    }

    if (ret == 0)
    {
// 成功
#if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "SDEL", key, NULL);
        if (res == 0)
        {
            // LOG("[AOF]: SDEL ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: SDEL ASYN FAIL\n");
        }
#endif
        return 0;
    }
}

int kvs_skiplist_all_del(skiplist_t *sl)
{
    if (!sl)
        return -1;

    int ret = skiplist_clear(sl);

    if (ret == -1)
    {
        return -1;
    }
    if (ret == 0)
    {
#if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "SDELA", NULL, NULL);
        if (res == 0)
        {
            // LOG("[AOF]: SDELA ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: SDELA ASYN FAIL\n");
        }
#endif
        return 0;
    }
}

int kvs_skiplist_mod(skiplist_t *sl, char *key, char *value)
{
    if (!sl || !key || !value)
        return -1;

    int ret = skiplist_update(sl, key, value);
    if (ret == 1)
    {
        // 不存在
        return 1;
    }
    if (ret == -1)
    {
        return -1;
    }
    if (ret == 0)
    {
#if AOF_PERSIST
        int res = aof_async_write(global_aof_writer, AOF_FILE_NAME, "SMOD", key, value);
        if (res == 0)
        {
            // LOG("[AOF]: SMOD ASYN SUCCESS\n");
        }
        else
        {
            LOG("[AOF]: SMOD ASYN FAIL\n");
        }
#endif
        // 成功
        return 0;
    }
}

int kvs_skiplist_exist(skiplist_t *sl, char *key)
{
    if (!sl || !key)
        return -1;
    SNode *buf = skiplist_search(sl, key);
    if (buf == NULL)
    {
        // 不存在
        return 1;
    }
    return 0;
}

int kvs_skiplist_count(skiplist_t *sl)
{
    if (!sl)
        return -1;
    int count = skiplist_get_size(sl);
    return count;
}